﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WinNemoLib;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ISODOC.Models
{
    public class History
    {
        public class History_DS
        {
            public string _Darno { get; set; }
            public string _DATTM { get; set; }
            public string _USER { get; set; }
            public string _TEXT { get; set; }
            public string _TXT_STATUS { get; set; }
        }
        public Boolean Ins_History(ConnectionWrapper cnn, History_DS incoming)
        {
            try
            {
                StringBuilder comstr = new StringBuilder();
                comstr.AppendLine(" INSERT INTO ");
                comstr.AppendLine(WinNemoLib.Nemo.GetINI("Dbase", "Base") + ".[dbo].[DARHISTORY]");
                comstr.AppendLine(" (DARNO,DATTM,[USER],TEXT,TEXT_STATUS) VALUES (");

                comstr.AppendLine(" @doctype,getdate(),@name,@text,@txtstatus) ");

                CommandWrapper cmd = cnn.CreateCommand();
                cmd.CommandText = comstr.ToString();
                cmd.AddParameterStringWithValue("@doctype", incoming._Darno);
                cmd.AddParameterStringWithValue("@name", incoming._USER.ToUpper());
                cmd.AddParameterStringWithValue("@txtstatus", incoming._TXT_STATUS.ToUpper());
                cmd.AddParameterStringWithValue("@text", incoming._TEXT.ToString());
                if (cmd.ExecuteNonQuery() <= 0)
                {
                    cnn.RollbackTransaction();
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }

        }

        public List<History_DS> LIST_HISTORY(string code)
        {

            string ConnectionString = WinNemoLib.Nemo.GetConfiguredDbConnectionWrappertostring("Database");

            SqlConnection cnn = new SqlConnection(ConnectionString);
            StringBuilder str = new StringBuilder();
            string wh = " where ";
            str.AppendLine(" select * from " + WinNemoLib.Nemo.GetINI("Dbase", "Base") + ".dbo.DARHISTORY  ");
            if (StrUtil.IsNotEmpty(code))
            {
                str.Append(wh + "  DARNO = '" + code + "'");
                wh = " and ";
            }


            SqlCommand cmd = new SqlCommand(str.ToString() + " order by DATTM desc", cnn);
            cmd.Connection.Open();
            cmd.CommandType = CommandType.Text;


            SqlDataAdapter my = new SqlDataAdapter();
            DataSet ds = new DataSet();
            my.SelectCommand = cmd;
            my.Fill(ds);
            List<History_DS> list = new List<History_DS>();
            if (ds.Tables[0].Rows.Count <= 0)
            {
                cmd.Connection.Close();
                return list;
            }
            History_DS model = new History_DS();
            for (int cuIndex = 0; cuIndex < ds.Tables[0].Rows.Count; cuIndex++)
            {
                DataRow items = ds.Tables[0].Rows[cuIndex];
                model = new History_DS();
                model._Darno = items["DARNO"].ToString();
                model._USER = items["USER"].ToString();
                model._TXT_STATUS = items["TEXT_STATUS"].ToString();
                model._TEXT = items["TEXT"].ToString();
                model._DATTM = DateUtil.ToDisplayDateTime(items["DATTM"]);
                list.Add(model);
            }

            return list;
        }

    }
}